//
//  FormData.m
//  Patrol
//
//  Created by 范武 on 13-2-27.
//  Copyright (c) 2013年 FSTI. All rights reserved.
//

#import "FormData.h"

@implementation FormData
- (id)initWithDictonary:(NSDictionary *)json
{
    self = [super init];
    if (self) {
        if (json[@"device_name"]) {
            self.deviceName = json[@"device_name"];
        }
        if (json[@"task_id"]) {
            self.taskID = [json[@"task_id"] integerValue];
        }
        if (json[@"plan_name"]) {
            self.planName = json[@"plan_name"];
        }
        if (json[@"form_fields"]) {
            NSArray * fields = json[@"form_fields"];
            NSMutableArray * formFields = [[NSMutableArray alloc] initWithCapacity:fields.count];
            for (NSDictionary * field in fields) {
                FormField * formField = [[FormField alloc] initWithDictonary:field];
                [formFields addObject:formField];
            }
            self.fieldData = formFields;
        }
    }
    return self;
}

- (NSString *)description
{
    NSMutableString * descriptionOutput = [[NSMutableString alloc] init];
    [descriptionOutput appendFormat: @"device_name = \"%@\"\r\n", _deviceName.description];
    
    [descriptionOutput appendFormat: @"form_fields = \"%@\"\r\n", _fieldData.description];
    
    [descriptionOutput appendFormat: @"plain_name = \"%@\"\r\n", _planName.description];
    
    [descriptionOutput appendFormat: @"task_id = \"%i\"\r\n", _taskID];
    return descriptionOutput;
}

/**
 *  获取每个field的用户输入，组装成Array串送往服务器提交
 */
- (NSArray *) getUserInputJson
{
    NSMutableArray * dicts = [NSMutableArray arrayWithCapacity:_fieldData.count];
    
    for (FormField * field in _fieldData) {
        NSDictionary * d = [field getUserInputJson];
        [dicts addObject:d];
    }
    return dicts;
}


- (BOOL) isUserInputAllTheNessaryFields
{
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"notNull == %i",YES];
    NSArray * notNullField = [_fieldData filteredArrayUsingPredicate:predicate];
    for (FormField * field in notNullField) {
        NSString * input = [field getUserInput];
        if (input == nil || input.length == 0) {
            return NO;
        }
    }
    return YES;
}

@end

@implementation FieldOption
- (NSString *)description
{
    NSString * desc = [NSString stringWithFormat:@"FieldOptions: {\n\t title: %@, \n\tvalue:%i\n}",_title,_value];
    return desc;
}
@end

@implementation FormField

/**
 *  根据用户输入生成用于提交到后台的字符串
 */
- (NSDictionary *)getUserInputJson
{
    NSString * value = [self getUserInput];
    if (!value) {
        value = @"";
    }
    NSString * key = self.fieldID;
    return @{key : value};
}

/**
 *  获取用户的输入
 */
- (NSString *)getUserInput
{
    NSString * value;
    switch (_type) {
        case radio:
            value = [self switchValue];
            break;
        case textField:
        case textView:
            value = [_fieldControl valueForKey:@"text"];
            break;
    }
    return value;
}

/**
 *  这么写其实是有一定bug的，相当于假定on代表正常，返回值为1；0代表异常，返回0
 */
- (NSString *) switchValue
{
    UISwitch * _switch = (UISwitch *)_fieldControl;
    if (_switch.on) {
        return @"1";
    }else{
        return @"0";
    }
}

- (NSString *)description
{
    NSMutableString * descriptionOutput = [[NSMutableString alloc] init];
    [descriptionOutput appendFormat: @"field_title = %@\r\n", _title];
    [descriptionOutput appendFormat: @"field_type = \"%i\"\r\n", _type];
    [descriptionOutput appendFormat: @"option_values: { %@ }\r\n", _optionValues.description];
    [descriptionOutput appendFormat: @"not_null = \"%i\"\r\n", _notNull];
    [descriptionOutput appendFormat: @"field_id = \"%@\"\r\n", _fieldID.description];
    
    return descriptionOutput;
}

- (id)copyWithZone:(NSZone *)zone
{
    FormField * f = [self.class allocWithZone:zone];
    f.fieldID = _fieldID;
    f.title = _title;
    if (_optionValues) {
        f.optionValues = _optionValues;
    }
    f.notNull = _notNull;
    f.type = _type;
    return f;
}

- (id)initWithDictonary:(NSDictionary *)json
{
    self = [super init];
    if (self) {
        if (json[@"field_title"]) {
            self.title = json[@"field_title"];
        }
        
        if (json[@"field_type"]) {
            NSInteger iType = [json[@"field_type"] integerValue];
            switch (iType) {
                case 1:
                    self.type = radio;
                    break;
                case 2:
                    self.type = textField;
                    break;
                case 3:
                    self.type = textView;
                    break;
            }
        }

        if (json[@"not_null"]) {
            BOOL notNull = [json[@"not_null"] boolValue];
            self.notNull = notNull;
        }else{
            self.notNull = NO;
        }
        
        if (json[@"field_id"]) {
            NSString * fieldID = json[@"field_id"];
            self.fieldID = fieldID;
        }
        
        if (self.type == radio && json[@"option_values"]) {
            NSDictionary * options = json[@"option_values"];
            NSMutableArray * optoinValues = [[NSMutableArray alloc] initWithCapacity:options.count];
            
            NSEnumerator * ator = [options keyEnumerator];
            NSString * key;
            
            while (key = [ator nextObject] ) {
                FieldOption * option = [FieldOption new];
                option.title = key;
                option.value = [options[key] integerValue];
                [optoinValues addObject:option];
            }
            self.optionValues = optoinValues;
        }
    }
    return self;
}
@end
